<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-list-swipe.css" />

</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            任务单配送
        </div>
    </header>
    <div class="aui-content">

        <div class="aui-form">

            <div class="aui-input-row" style="font-size: 18px;">
                <span class="aui-input-addon"
                    style="width: 20%;text-align: left;font-size: 0.8rem;font-weight: 600;">配送任务号:</span>
                <input type="text" class='aui-input' placeholder="" style="width: 80%;" id="paTaskCode" />
                <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="searchTask()"></i>
            </div>

        </div>
        <div style="background-color:white; height: 100%;width: 100%;font-size: 16px;">
            <table style="width:100%;margin-left: 5px;">
                <tr>
                    <td style="width: 25%;">配送单号：</td>
                    <td id="code" colspan="3"></td>
                </tr>
                <tr>
                </tr>
                <tr>
                    <td style="width: 25%;">目标仓库：</td>
                    <td id="uda1c" style="width: 75%;"></td>

                </tr>
                <tr>
                    <td style="width: 25%;">调入库位：</td>
                    <td id="ext06" style="width: 75%;"></td>
                </tr>
                <tr>
                    <td style="width: 25%;">创建时间：</td>
                    <td id="createDate" colspan="3"></td>
                </tr>
                <tr>
                    <td style="width: 25%;">工单数量：</td>
                    <td id="workOrderCount" colspan="3"></td>
                </tr>
            </table>
        </div>
        <ul class="aui-list aui-form-list">

            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <input type="text" class="aui-input" placeholder="请扫描箱号" style="width: 80%;font-size: 16px;"
                        id="boxnum" onclick="getIdSelector();" />
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="padding-right:10px;font-size: 1.6rem"
                        onclick="doScan();"></i>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="doSearch()">查询</div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">

            <div style="background-color:white; height: 100%;width: 100%;">

                <ul class="ui-selectee" id="tbodyHtml">

                </ul>
            </div>
        </div>


</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../script/aui-list-swipe.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var mrlCodeSel = "";
    var list = [];
    apiready = function () {
        $("#paTaskCode").focus();
        idSelector = "paTaskCode";
        //查询条码解析规则
        initScanRule();
    };

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            if (idSelector == 'boxnum') {
                tmSplit(ret);
            }
        });
    }

    //扫描之后条码解析
    function tmSplit(sm) {
        //直接解析
        if (scanRule == "old") {
            //本地解析
            localAnalyze(sm);
        } else {
            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        var mrlCode = "";
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            mrlCode = arr[1];
            mrlCodeSel = arr[1];
        }
        var qty = "";
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            qty = arr[2];
        }
        var lotCode = "";
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            lotCode = arr[3];
        }
        var tmID = "";
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmID").val("");
        } else {
            tmID = arr[4];
        }
        var boxnum = "";
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            boxnum = arr[9];
        }
        $("#boxnum").val(mrlCodeSel);
        doSearch()
    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }
                    $("#boxnum").val("");
                    //物料号
                    var mrlCode = map["mrlCode"];
                    mrlCodeSel = map["mrlCode"];
                    //批次号
                    var lotCode = map["lotCode"];
                    //转移数量数量
                    var qty = map["qty"];
                    //条码
                    var tm = map["tm"];
                    //箱号
                    var boxnum = map["boxnum"];
                    //tmID
                    var tmID = map["tmID"];
                    console.log(tm);
                    $("#boxnum").val(mrlCodeSel);
                    doSearch()
                } else if (myobj.code == '1') {
                    $("#boxnum").attr('disabled', false);
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }


    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        if (!clickThrottle()) {
            $("#boxnum").val("");
            return;
        }
        var keycode = e.keyCode;
        var tm = $(this).val();
        $("#boxnum").val("");
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                //调用条码解析+保存事件
                tmSplit(tm);
            }
        }
    });

    //查询
    function doSearch() {
        mrlCodeSel = $("#boxnum").val();
        var arr = [];
        for (var i = 0; i < list.length; i++) {
            if (list[i].mrlCode == mrlCodeSel) {
                arr.push(list[i]);
            }
        }
        var paTaskCode = $("#paTaskCode").val();
        if (mrlCodeSel == "") {
            setTable(list, paTaskCode);
        } else {
            setTable(arr, paTaskCode);
        }

    }





    var swipe = new ListSwipe();
    var paTaskCode = "";


    //扫描回车事件
    $("#paTaskCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            $("#paTaskCode").val(sm);
            getDistributeMrlInfo(sm);
        }
    });


    // 获取工配送任务单-对应的 物料需求信息
    function getDistributeMrlInfo(paTaskCode) {
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchDistributeMrlInfo";
        var params = new Object();
        params.url = url;
        params.method = 'post';
        params.dataType = 'json';
        var un = localStorage.getItem('un');
        var data = {
            values: {
                paTaskCode: paTaskCode,
                un: un
            }
        };
        console.log("un", un);
        api.showProgress({
            title: '查询中...',

        });
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                $("#paTaskCode").focus();
                idSelector = "paTaskCode";
                //返回成功
                if (myobj.code == "0") {
                    list = myobj.list;
                    var mainData = myobj.mainData;
                    var code = mainData.code;
                    var workOrderCode = mainData.workOrderCode;
                    var uda1c = mainData.uda1c;
                    var uda2c = mainData.uda2c;

                    var ext06 = mainData.ext06;
                    var workOrderCount = mainData.workOrderCount;
                    var createDate = mainData.createDate;
                    var taskType = mainData.taskType;
                    var status = mainData.status;

                    workOrderCode = workOrderCode == null ? "" : workOrderCode;
                    uda2c = uda2c == null ? "" : uda2c;
                    if (status == '0') {
                        status = '未开始'
                    }
                    if (status == '1') {
                        status = '进行中'
                    }
                    if (status == '2') {
                        status = '已完成'
                    }
                    if (status == '3') {
                        status = '已取消'
                    }
                    $("#code").text(paTaskCode);
                    $("#ext06").text(ext06);
                    $("#workOrderCount").text(workOrderCount);
                    $("#uda1c").text(uda1c);
                    $("#createDate").text(createDate);
                    $("#taskType").text(taskType);
                    $("#status").text(status);
                    setTable(list, paTaskCode);

                } else if (myobj.code == "1") {
                    //失败
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

    }



    function setTable(list, code) {
        var html = "";
        for (var i = 0; i < list.length; i++) {
            var uda1 = list[i].receiveQty;
            var bgColor = "#F5F5F5";
            uda1 = (uda1 == null || uda1 == undefined || uda1 == "") ? "0" : uda1;
            if (Number(list[i].countQty) > Number(uda1)) {
                bgColor = "#FF5151";
            } else {
                bgColor = "#00BB00";
            }
            uda1 = (uda1 == null || uda1 == undefined || uda1 == "") ? "0" : uda1;
            html += "<div style='padding-top:4%;'></div>";
            html += "<li class='sortable ui-selectee ui-sortable' >";
            html += "<ul class='aui-list aui-media-list ui-selectee'>";
            html += "<li class='aui-list-item  ui-selectee' style='color: black !important;background-color:" + bgColor + " !important;'>";
            html += "<div class='aui-media-list-item-inner ui-selectee' >";
            html += "<div class='aui-list-item-inner ui-selectee'>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料号:</div>";
            html += "<div class='aui-list-item-inner ui-selectee'  style='color:#000000;'>" + list[i].mrlCode + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>物料名:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000; word-wrap:break-word; overflow:hidden;'>" + list[i].mrlName + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'>计算数量:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + list[i].countQty + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'style = 'word-break:break-all'>已备货数量:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" + uda1 + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'style = 'word-break:break-all'>已接收数量:</div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'>" +  list[i].ext15 + "</div>";
            html += "</div>";
            html += "<div class='aui-list-item-text ui-selectee' style='height: 15px;'>";
            html += "<div class='aui-list-item-label ui-selectee list-tr-title'style = 'word-break:break-all'></div>";
            html += "<div class='aui-list-item-inner ui-selectee' style='color:#000000;'></div>";
            html += "<div class='aui-list-item-right aui-btn ui-selectee' style='background:#d0d0d0;color:#000000;' onclick='goDetailPge(\"" + list[i].mrlCode + "\",\"" + code + "\",\"" + list[i].mrlName + "\");'>查看</div>";
            html += "<div class='aui-list-item-right aui-btn ui-selectee' style='background:#d0d0d0;color:#000000;' onclick='goDetailBHPge(\"" + list[i].mrlCode + "\",\"" + code + "\",\"" + list[i].mrlName + "\");'>备货</div>";
            html += "</div>";
            html += "</div>";
            html += "</div>";
            html += "</li>";
            html += "</ul>";
            html += "</li>";
        }
        $api.html($api.byId("tbodyHtml"), html);
        $("#boxnum").focus();
        idSelector = "boxnum";
    };

    //跳转任务明细页面
    function goDetailPge(mrlCode, paTaskCode, mrlName) {
        var url = "paAndonDistribute-detail.html";
        api.openWin({
            name: 'paAndonDistribute-detail',
            url: 'paAndonDistribute-detail.html',
            pageParam: {
                paTaskCode: paTaskCode,
                mrlName: mrlName,
                mrlCode: mrlCode
            }
        });
    }


    //跳转备货页面
    function goDetailBHPge(mrlCode, paTaskCode, mrlName) {
        var url = "paAndonDistribute-detail-bh.html";
        api.openWin({
            name: 'paAndonDistribute-detail-bh',
            url: 'paAndonDistribute-detail-bh.html',
            pageParam: {
                paTaskCode: paTaskCode,
                mrlName: mrlName,
                mrlCode: mrlCode
            }
        });
    }


    function createPaTaskDetail(str) {
        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/createPaTaskDetail";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                paTaskCode: str,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                console.log(JSON.stringify(myobj));
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();

                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                } else {
                    api.hideProgress();
                    if (myobj.message != 'EE') {
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                    }

                }
            }
        });


    }

    function searchTask() {
        api.openWin({
            name: 'paSearchTask',
            url: 'paSearchTask.html',
            pageParam: {
                name: "paAndonDistribute"
            }
        });
    }

    function setPaTaskCode(str) {
        $("#paTaskCode").val(str);
        getDistributeMrlInfo(str);
        createPaTaskDetail(str);

    }
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }


</script>

</html>